package com.qxf.service.actions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.qxf.dao.StatisticsDao;
import com.qxf.exception.UserConfigurationException;
import com.qxf.model.ReturnEvent;
import com.qxf.model.beans.statistic.MediaStatistic;
import com.qxf.model.beans.statistic.StatisticBase;
import com.qxf.service.BaseAction;
import com.qxf.service.jfreechart.ChartData;
import com.qxf.service.jfreechart.ChartHelper;
import com.qxf.service.jfreechart.PieStatisticData;
import com.qxf.service.jfreechart.StatisticData;
import com.qxf.util.Constants;
import com.qxf.util.StatisticType;

public class StatisticsAction extends BaseAction {

	private StatisticsDao staDao;
	
	static Logger log = Logger.getLogger(StatisticsAction.class);
	
	@Override
	public ReturnEvent process(HttpServletRequest req, HttpServletResponse resp) {
		String op = req.getParameter("op");
		log.info("op = " + op);
		ReturnEvent responseEvent = new ReturnEvent();
		Object data = null;
		staDao = new StatisticsDao();
		try {
			if("refreshData".equalsIgnoreCase(op)) {
				data = refreshData(req, resp);
			}
		} catch(UserConfigurationException uce) {
			responseEvent.setReturnCode(Constants.RETURN_FAILURE);
			responseEvent.setErrorMessage(uce.getMessage());
			log.info("UserConfigurationException: " + uce.getMessage());
		} catch (Exception e) {
			responseEvent.setReturnCode(Constants.RETURN_FAILURE);
			e.printStackTrace();
		}
		if(data != null) {
			responseEvent.setReturnCode(Constants.RETURN_SUCCESS);
			responseEvent.setResult(data);
		} else {
			responseEvent.setReturnCode(Constants.RETURN_FAILURE);
		}
		return responseEvent;
	}
	
	private List<StatisticBase> refreshData(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String reqList = req.getParameter("reqList");
		String activityId = userSession.getActivityInfo().getActivityId();
		log.info("refreshData(): reqList=" + reqList + ", activityId=" + activityId);
		
		List<StatisticBase> statisticList = new ArrayList<StatisticBase>();
		StatisticBase statisticInfo = null;
		int reqIndex;
		if(null != activityId && activityId.length() > 0
				&& null != reqList && reqList.length() > 0) {
			String[] reqAry = reqList.split(",");
			for(int i = 0; i < reqAry.length; i++) {
				reqIndex = Integer.parseInt(reqAry[i]);
				if(StatisticType.getType(reqIndex).equals(StatisticType.MEDIA)){
					log.info("refreshData(): 获取媒体统计数据");
					statisticInfo = staDao.getMediaStatistic(activityId);
				} else if(StatisticType.getType(reqIndex).equals(StatisticType.AD)){
					log.info("refreshData(): 获取广告位统计数据");
					statisticInfo = staDao.getAdStatistic(activityId);
				}
				log.info("refreshData(): 创建统计图");
				createChart(statisticInfo, req);
				if(null != statisticInfo) {
					statisticList.add(statisticInfo);
				}
			}
		}
		return statisticList;
	}
	
	private void createChart(StatisticBase statisticInfo, HttpServletRequest request) throws Exception {
		if(null == statisticInfo) return;
		
		if(StatisticType.getType(statisticInfo.getsType()).equals(StatisticType.MEDIA)){
			log.info("createChart(): 创建媒体统计图");
			PieStatisticData chartInfo = null;
			MediaStatistic statistic = (MediaStatistic)statisticInfo;
			chartInfo = new PieStatisticData();
			chartInfo.setChartName(statistic.gettData().get(0).getChartName());
			statistic.setcData(getChartResult(chartInfo, statistic.gettData(),"pie",request));
			
			log.info("createChart(): 创建各媒体广告位统计图");
			List<ChartData> cDataList = new ArrayList<ChartData>();
			for(List<StatisticData> adList:statistic.getAdList()) {
				chartInfo = new PieStatisticData();
				chartInfo.setChartName(adList.get(0).getChartName());
				cDataList.add(getChartResult(chartInfo, adList,"pie",request));
			}
			statistic.setcDataList(cDataList);
			statisticInfo = statistic;
		} else if(StatisticType.getType(statisticInfo.getsType()).equals(StatisticType.AD)){
			log.info("createChart(): 创建广告位统计图");
			
		}
	}

	private ChartData getChartResult(StatisticData chartInfo, List<StatisticData> list, String chartType, HttpServletRequest request) throws IOException {
		ChartHelper helper = new ChartHelper(chartInfo, list, request);
		String tempPath = "";
		if("pie" == chartType) {
			tempPath = helper.getPieChart();
		} else if("bar" == chartType) {
			tempPath = helper.getBarChart();
		}
		ChartData chartData = new ChartData();
		chartData.setChartName(chartInfo.getChartName());
		chartData.setChartTempPath(tempPath);
		return chartData;
	}

}
